package com.qiantu.phone.ui.activity;

import android.content.Intent;
import android.view.View;

import com.qiantu.api.db.LLDeviceDBManager;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.entity.DeviceCustomKeyBean;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.ui.uiutil.DeviceType;
import com.qiantu.phone.widget.btnview.OneButtonView;
import com.qiantu.phone.widget.btnview.OneTwoThreeFourSixEightNineButtonView;

import java.util.List;

public class KeyCustomActivity extends AppActivity {

    private OneButtonView btnView1;
    private OneTwoThreeFourSixEightNineButtonView btnView2;
    private OneTwoThreeFourSixEightNineButtonView btnView3;
    private OneTwoThreeFourSixEightNineButtonView btnView4;
    private OneTwoThreeFourSixEightNineButtonView btnView8;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_key_custom;
    }

    @Override
    protected void initView() {
        btnView1 = findViewById(R.id.btn_view_1);
        btnView2 = findViewById(R.id.btn_view_2);
        btnView2 = findViewById(R.id.btn_view_3);
        btnView4 = findViewById(R.id.btn_view_4);
        btnView8 = findViewById(R.id.btn_view_8);
    }

    @Override
    public void onRightClick(View view) {
        Intent intent = new Intent();
        intent.putExtra("DATA", "");
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    protected void initData() {
        String deviceSerialNo = getIntent().getStringExtra("deviceSerialNo");
        DeviceBean device = LLDeviceDBManager.getInstance(getContext()).getDeviceBySerialNo(deviceSerialNo);
        if (device.getDeviceType().equals(DeviceType.WALL_SWITCH1.stringType)||device.getDeviceType().equals(DeviceType.WALL_SWITCH_S1.stringType)) {
            btnView1.setVisibility(View.VISIBLE);
            btnView2.setVisibility(View.GONE);
            btnView3.setVisibility(View.GONE);
            btnView4.setVisibility(View.GONE);
            btnView8.setVisibility(View.GONE);
        } else if (device.getDeviceType().equals(DeviceType.WALL_SWITCH2.stringType) || device.getDeviceType().equals(DeviceType.WALL_SWITCH_S2.stringType)) {
            btnView1.setVisibility(View.GONE);
            btnView2.setVisibility(View.VISIBLE);
            btnView3.setVisibility(View.GONE);
            btnView4.setVisibility(View.GONE);
            btnView8.setVisibility(View.GONE);
        } else if (device.getDeviceType().equals(DeviceType.WALL_SWITCH3.stringType) || device.getDeviceType().equals(DeviceType.WALL_SWITCH_S3.stringType)) {
            btnView1.setVisibility(View.GONE);
            btnView2.setVisibility(View.GONE);
            btnView3.setVisibility(View.VISIBLE);
            btnView4.setVisibility(View.GONE);
            btnView8.setVisibility(View.GONE);
        }else if (device.getDeviceType().equals(DeviceType.WALL_SWITCH4.stringType) || device.getDeviceType().equals(DeviceType.WALL_SWITCH_J4.stringType) || device.getDeviceType().equals(DeviceType.WALL_SWITCH_S4.stringType)) {
            btnView1.setVisibility(View.GONE);
            btnView2.setVisibility(View.GONE);
            btnView3.setVisibility(View.GONE);
            btnView4.setVisibility(View.VISIBLE);
            btnView8.setVisibility(View.GONE);
        } else if (device.getDeviceType().equals(DeviceType.WALL_SWITCH8.stringType) || device.getDeviceType().equals(DeviceType.WALL_SWITCH_J8.stringType)) {
            btnView1.setVisibility(View.GONE);
            btnView2.setVisibility(View.GONE);
            btnView3.setVisibility(View.GONE);
            btnView4.setVisibility(View.GONE);
            btnView8.setVisibility(View.VISIBLE);
        }
        updateCustomKey(device.getDeviceCustomKeys());
    }

    private void updateCustomKey(List<DeviceCustomKeyBean> customKeyList ){
        String defaultName = getString(R.string.custom_name);
        String key1 = defaultName;
        String key2 = defaultName;
        String key3 = defaultName;
        String key4 = defaultName;
        String key5 = defaultName;
        String key6 = defaultName;
        String key7 = defaultName;
        String key8 = defaultName;
        if (customKeyList != null) {
            for (DeviceCustomKeyBean keyData : customKeyList) {
                switch (keyData.getKeyIndex()) {
                    case "1":
                        key1 = keyData.getKeyName();
                        break;
                    case "2":
                        key2 = keyData.getKeyName();
                        break;
                    case "3":
                        key3 = keyData.getKeyName();
                        break;
                    case "4":
                        key4 = keyData.getKeyName();
                        break;
                    case "5":
                        key5 = keyData.getKeyName();
                        break;
                    case "6":
                        key6 = keyData.getKeyName();
                        break;
                    case "7":
                        key7 = keyData.getKeyName();
                        break;
                    case "8":
                        key8 = keyData.getKeyName();
                        break;
                }
            }
        }
        btnView1.setText(key1);
        btnView2.setText(key1, key2);
        btnView2.setText(key1, key2, key3);
        btnView4.setText(key1, key2, key3, key4);
        btnView8.setText(key1, key2, key3, key4, key5, key6, key7, key8);
    }
}